﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

public partial class Admin_Product_ProductEdit : BLL.AdminPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BLL.DDLManage.BindProductType(ddlType);
            var a = BLL.Common.GetSafeRequest(Page);
            ViewState["ID"] = a;
            BindData();
        }
    }
    private void BindData()
    {

        var id = ViewState["ID"].ToString();
        var a = ctx.T_Product.Single(p => p.ID.ToString().Equals(id));
        //txtAuthor.Text = a.Author;
        //txtIntro.Text = a.Introduce;
        //txtPrice.Text = a.Price;
        //txtProductName.Text = a.ProductName;
        //txtSize.Text = a.Size;
        //txtYears.Text = a.Years;
        //ddlType.SelectedValue = a.PTID.ToString();
        //MyImg.ImageUrl = "~/uploads/" + a.PhotoName;
        //hfImg.Value = a.PhotoName;

        txtTitle.Text = a.ProductTitle;
        txtAbout.Text = a.ProductAbout;
        txtDetails.Text = a.ProductDetails;
        txtStartPrice.Text = a.StartPrice;
        txtStartTime.Text = a.StartTime.ToString() ;
        txtEndTime.Text = a.EndTime.ToString();
        MyImg.ImageUrl = "~/uploads/" + a.CoverIMG;
        hfImg.Value = a.CoverIMG;
        MyImg1.ImageUrl = "~/uploads/" + a.ProductDetailsIMG;
        hfImg1.Value = a.CoverIMG;

    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {

        var id = ViewState["ID"] == null ? "0" : ViewState["ID"].ToString();
        if (id != "0")
        {
            var product = ctx.T_Product.Single(p => p.ID.ToString().Equals(id));
            //product.ProductName = txtProductName.Text;
            //product.PhotoName = hfImg.Value;
            //product.Introduce = txtIntro.Text;
            //product.Author = txtAuthor.Text;
            //product.Years = txtYears.Text;
            //product.Size = txtSize.Text;
            //product.Price = txtPrice.Text;
            //product.PTID = Convert.ToInt32(ddlType.SelectedValue);

            product.ProductTitle = txtTitle.Text;
            product.ProductAbout = txtAbout.Text;
            product.CoverIMG = hfImg.Value;
            product.ProductDetails = txtDetails.Text;
            product.ProductDetailsIMG = hfImg1.Value;
            product.StartPrice = txtStartPrice.Text;
            product.CurrentPrice = txtStartPrice.Text;
            product.StartTime = Convert.ToDateTime(txtStartTime.Text);
            product.EndTime = Convert.ToDateTime(txtEndTime.Text);

            ctx.SubmitChanges();
            Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改成功!');", true);
            BindData();
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改失败!');", true);
        }
    }
}